import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author colafries
 * @version v1.0
 * @ClassName test
 * @Description 解析给定字符串中的表达式，表达式的格式是以花括号包裹的内容 create by chatGPT
 * @date Created in 2024/3/31
 **/
public class ExpressionParser {
    static Map<String, String> configColMap = new HashMap<>();
    static {
        configColMap.put("id1", "col1");
        configColMap.put("id2", "col2");
        configColMap.put("id3", "col3");
        configColMap.put("id4", "col4");
    }

    public static void main(String[] args) {
        String expression = "({id1} + {id2}) / {id1} + ({id3} - {id4})";
        String parsedExpression = parseExpression(expression);
        System.out.println("Parsed Expression: " + parsedExpression);

    }

    public static String parseExpression(String expression) {
        Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}");
        Matcher matcher = pattern.matcher(expression);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String key = matcher.group(1);
            String value = configColMap.getOrDefault(key, key);
            matcher.appendReplacement(sb, value);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}
